﻿using System;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace SystemDiagramNew.TypeDefinition
{
	public class EdgeInfo
	{
		public EdgeInfo(EdgeInfo rhs)
		{
			this.StartPoint = rhs.StartPoint;
			this.EndPoint = rhs.EndPoint;
			this.Bulge = rhs.Bulge;
		}

		public EdgeInfo(XYZ startPoint, XYZ endPoint, double bulge)
		{
			this.StartPoint = startPoint;
			this.EndPoint = endPoint;
			this.Bulge = bulge;
		}

		public EdgeInfo(Curve curve)
		{
			if (curve.GetType() == typeof(Line))
			{
				this.StartPoint = curve.GetEndPoint(0);
				this.EndPoint = curve.GetEndPoint(1);
				this.Bulge = 0.0;
				return;
			}
			if (curve.GetType() == typeof(Arc))
			{
				Arc arc = curve as Arc;
				this.StartPoint = curve.GetEndPoint(0);
				this.EndPoint = curve.GetEndPoint(1);
				this.Bulge = Geometry.GetBulge(this.StartPoint, this.EndPoint, arc.Center, arc.Normal.IsAlmostEqualTo(XYZ.BasisZ));
			}
		}

		public double Length()
		{
			if (this.IsArc)
			{
				XYZ xyz = null;
				Geometry.GetCenterWithBulge(this.StartPoint, this.EndPoint, this.Bulge, ref xyz);
				double num = xyz.DistanceTo(this.StartPoint);
				double angle = Geometry.getAngle(xyz, this.StartPoint);
				double angle2 = Geometry.getAngle(xyz, this.EndPoint);
				double num2 = Geometry.BetweenTheAngles(angle, angle2, true);
				if (Geometry.LessThan(this.Bulge, 0.0))
				{
					num2 = Geometry.BetweenTheAngles(angle2, angle, true);
				}
				return num2 * num;
			}
			return this.StartPoint.DistanceTo(this.EndPoint);
		}

		internal Line CreateLegendLine(Transform transform)
		{
			return Geometry.CreateLine(this.StartPoint.GetXY(), this.EndPoint.GetXY(), transform);
		}

		public XYZ StartPoint { get; set; }

		public XYZ EndPoint { get; set; }

		public double Bulge { get; set; }

		public bool IsArc
		{
			get
			{
				return !Geometry.IsEqual(this.Bulge, 0.0);
			}
		}
	}
}
